import request from "@/utils/request";
import type { SysRoleDataPermissionDTO, SysRoleDataPermissionVO, SysRoleDTO, SysRoleFiltDTO, SysRoleVO } from "./sysRoleType";
import type { IdDTO, PageQueryDTO, PageQueryVO } from "../type/baseType";

enum URL {
    URL_ROLE_BASE = '/role',
    URL_ROLE_PAGE = URL_ROLE_BASE + '/page',
    URL_ROLE_DATA_PERM = URL_ROLE_BASE + '/dataPerm',
    URL_ROLE_DATA_PERM_SUPPORT = URL_ROLE_DATA_PERM + '/support'
}

/**
 * 增加Role
 * @param sysRoleDTO
 * @returns 
 */
export const addRole = (sysRoleDTO: SysRoleDTO):
    Promise<undefined> =>
    request.post(URL.URL_ROLE_BASE, sysRoleDTO)

/**
 * 删除Role
 * @param id
 * @returns 
 */
export const deleteRole = (id: IdDTO):
    Promise<undefined> =>
    request.delete(`${URL.URL_ROLE_BASE}/${id.id}`)

/**
 * 修改Role
 * @param id
 * @param sysRoleDTO
 * @returns 
 */
export const updateRole = (id: IdDTO, sysRoleDTO: SysRoleDTO):
    Promise<undefined> =>
    request.put(`${URL.URL_ROLE_BASE}/${id.id}`, sysRoleDTO)

/**
 * 查询Role
 * @param id
 * @returns 
 */
export const queryRole = (id: IdDTO):
    Promise<SysRoleVO> =>
    request.get(`${URL.URL_ROLE_BASE}/${id.id}`)

/**
 * 分页查询当前登录用户有权访问的所有角色列表
 * @param pageQueryDTO
 * @returns 
 */
export const queryRolePage = (pageQueryDTO: PageQueryDTO<SysRoleFiltDTO>):
    Promise<PageQueryVO<SysRoleVO>> =>
    request.get(URL.URL_ROLE_PAGE, {
        params: {
            pageNum: pageQueryDTO.pageNum,
            pageSize: pageQueryDTO.pageSize,
            ...pageQueryDTO.param
        }
    })

/**
 * 查询Role支持的数据权限
 * @param id
 * @returns 
 */
export const queryRoleSupportDataPerm = ():
    Promise<string[]> =>
    request.get(URL.URL_ROLE_DATA_PERM_SUPPORT)

/**
 * 修改Role数据权限
 * @param id
 * @param sysRoleDataPermissionDTO
 * @returns 
 */
export const updateRoleDataPerm = (id: IdDTO, sysRoleDataPermissionDTO: SysRoleDataPermissionDTO):
    Promise<undefined> =>
    request.put(`${URL.URL_ROLE_DATA_PERM}/${id.id}`, sysRoleDataPermissionDTO)

/**
 * 查询Role数据权限
 * @param id
 * @returns 
 */
export const queryRoleDataPerm = (id: IdDTO):
    Promise<SysRoleDataPermissionVO> =>
    request.get(`${URL.URL_ROLE_DATA_PERM}/${id.id}`)